package com.hexiang.service;

import com.hexiang.DemoService;
import com.hexiang.entity.User;
import com.hexiang.entity.UserRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * Dubbo示例服务接口实现
 *
 * @author h01463
 * @dubbo
 */
@DubboService
@Slf4j
public class DemoServiceImpl implements DemoService {
    /**
     * sayHello
     *
     * @param name 用户名
     * @return
     * @mock zhangsan
     */
    @Override
    public String sayHello(String name) {
        log.info("sayHelloStart,name={}", name);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("sayHelloInterruptedException", e);
            Thread.currentThread().interrupt();
        }
        String resp = "Hello, " + name;
        log.info("sayHelloEnd,resp={}", resp);
        return resp;
    }

    /**
     * sayHelloAsync
     *
     * @param name 用户名
     * @return
     * @mock zhangsan
     */
    @Override
    public CompletableFuture<String> sayHelloAsync(String name) {
        log.info("sayHelloAsyncStart,name={}", name);
        // 建议为supplyAsync提供自定义线程池，避免使用JDK公用线程池
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                log.error("sayHelloAsyncInterruptedException", e);
                Thread.currentThread().interrupt();
            }
            String resp = name + ",sayHelloAsyncResponse";
            log.info("sayHelloAsyncEnd,resp={}", resp);
            return resp;
        });

    }

    /**
     * sayHelloCompletableFuture
     *
     * @param name 用户名
     * @return
     * @mock zhangsan
     */
    @Override
    public CompletableFuture<String> sayHelloCompletableFuture(String name) {
        log.info("sayHelloCompletableFutureStart,name={}", name);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("Interrupted", e);
            Thread.currentThread().interrupt();
        }
        return CompletableFuture.completedFuture(name + ",asyncResponse");
    }

    /**
     * getUser
     *
     * @param request com.hexiang.entity.UserRequest
     * @return
     */
    @Override
    public User getUser(UserRequest request) {
        return new User(request.getUserId(), "test", 22, "liuyang", LocalDateTime.now());
    }

    /**
     * timeout
     *
     * @param name 用户名
     * @return
     * @mock zhangsan
     */
    @Override
    public String timeout(String name) {
        log.info("timeoutStart,name={}", name);

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            log.error("InterruptedException", e);
            Thread.currentThread().interrupt();
        }

        String resp = name + ",ok";
        log.info("sayHelloAsyncEnd,resp={}", resp);
        return resp;
    }

    /**
     * asyncTimeout
     *
     * @param name 用户名
     * @return
     * @mock zhangsan
     */
    @Override
    public CompletableFuture<String> asyncTimeout(String name) {
        log.info("asyncTimeoutStart,name={}", name);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            log.error("InterruptedException", e);
            Thread.currentThread().interrupt();
        }
        return CompletableFuture.completedFuture(name + ",asyncResponse");
    }

    /**
     * err
     *
     * @param name 用户名
     * @return
     * @mock zhangsan
     */
    @Override
    public String err(String name) {
        log.info("errStart,name={}", name);
        throw new RuntimeException("this is err");
    }

}